package com.example.dql.diary.PictureSelection;

import android.content.Intent;
import android.net.Uri;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.dql.diary.Utils.AnimationUtils;
import com.example.dql.diary.Model.PicDTO;
import com.example.dql.diary.R;

import java.util.ArrayList;
import java.util.List;

public class LookPicActivity extends AppCompatActivity {
    private PhotoViewPager viewPager;
    private TextView textView;
    private ImageView left_img;
    private ImageView right_img;
    private LinearLayout title;
    private List<Uri> imgList = new ArrayList<>();
    private List<PicDTO> getList = new ArrayList<>();
    private List<String> getPicList = new ArrayList<>();
    private String position;
    private int location;
    private boolean isClick = true;
    private MyImageAdapter adapter ;
    private static final int REQUEST_CODE_BACKLOOKPIC = 14;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_look_pic);
        textView = (TextView)findViewById(R.id.tx_lookPicNum);
        title = (LinearLayout)findViewById(R.id.myTitleBar);
        left_img = (ImageView)findViewById(R.id.left_img);
        right_img = (ImageView)findViewById(R.id.right_img);
        viewPager = (PhotoViewPager)findViewById(R.id.viewPage);
        getPicList = getIntent().getStringArrayListExtra("imgList");
        position = getIntent().getStringExtra("position");
        left_img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                goBack();

            }
        });
        right_img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                deletePic();
            }

        });
        for (int i =0 ;i<getPicList.size();i++){
            Uri uri = Uri.parse(getPicList.get(i));
            imgList.add(uri);
        }
        adapter = new MyImageAdapter(imgList, this, new MyImageAdapter.onPicClickListener() {
            @Override
            public void onPicClick() {
                if (isClick){
                    isClick = false;
                    title.setVisibility(View.INVISIBLE);
                    title.setAnimation(AnimationUtils.moveToGone());
                }else {
                    isClick = true;
                    title.setVisibility(View.VISIBLE);
                    title.setAnimation(AnimationUtils.moveToVisible());
                }
            }
        });
        viewPager.setAdapter(adapter);
        viewPager.setCurrentItem(Integer.valueOf(position));
        textView.setText((Integer.valueOf(position)+1) +" / "+ imgList.size());
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                textView.setText((position+1) +" / "+ imgList.size());
                location = position;
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void goBack() {
        Intent intent = new Intent(LookPicActivity.this,
                SelectPicActivity.class);
        intent.putStringArrayListExtra("requestList",(ArrayList<String>) getPicList);
        setResult(REQUEST_CODE_BACKLOOKPIC,intent);
        finish();
    }

    private void deletePic() {

        if (location==0){
            //初始位置
           int firstPosition = Integer.valueOf(position);
            imgList.remove(firstPosition);
            getPicList.remove(firstPosition);
            if (imgList.size()==0){
                goBack();
            }else {
                textView.setText((firstPosition+1) +" / "+ imgList.size());
            }

        }else {
            imgList.remove(location);
            getPicList.remove(location);
            textView.setText((location+1) +" / "+ imgList.size());
        }

        adapter.notifyDataSetChanged();
    }

    @Override
    public void onBackPressed() {
        Intent intent = new Intent(LookPicActivity.this,
                SelectPicActivity.class);
        intent.putStringArrayListExtra("requestList",(ArrayList<String>) getPicList);
        setResult(REQUEST_CODE_BACKLOOKPIC,intent);
        finish();
    }
}
